package xyz.liut.volley;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

/**
 * Created by liut on 2016/5/30.
 */
public class LiutNetworkImageView extends NetworkImageView {

    public LiutNetworkImageView(Context context) {
        super(context);
    }

    public LiutNetworkImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LiutNetworkImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 利用NetworkImageView显示网络图片
     */
    public void setImageUrl(String url) {

        final LiutVolley volley = LiutVolley.getInstance(getContext());

        ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
            @Override
            public void putBitmap(String key, Bitmap value) {
                volley.getCache().put(key, value);
            }

            @Override
            public Bitmap getBitmap(String key) {
                return volley.getCache().get(key);
            }
        };

        setImageUrl(url, new ImageLoader(volley.getRequestQueue(), imageCache));
    }

}
